package nsu.edu.zsq.controller.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import nsu.edu.zsq.bean.SchMajor;
import nsu.edu.zsq.bean.StuReward;
import nsu.edu.zsq.bean.Student;
import nsu.edu.zsq.common.JsonData;
import nsu.edu.zsq.exception.ParamException;
import nsu.edu.zsq.param.PageQuery;
import nsu.edu.zsq.service.sys.CourseService;
import nsu.edu.zsq.service.sys.SchMajorService;
import nsu.edu.zsq.service.sys.StudentRewardService;
import nsu.edu.zsq.service.sys.StudentService;

/** 
* Description: 学生奖惩信息的controller类<br>
*/
@RequestMapping("/sys/student/reward")
@Controller
public class StudentRewardController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private SchMajorService schMajorService;
    @Autowired
    private StudentRewardService studentRewardService;
    
    /**
     * 获取院系+专业的tree结构数据
     * @return JsonData
     */
    @RequestMapping("/schOrgWithMajorTree")
    @ResponseBody
    public JsonData schOrgWithMajorTree() {
        String listSchOrgWithMajorTree = courseService.listSchOrgWithMajorTree();
        List<SchMajor> listSchMajor = schMajorService.listSchMajor();
        Map<String, Object> map = new HashMap<String, Object>(2);
        map.put("listSchOrgWithMajorTree", listSchOrgWithMajorTree);
        map.put("listSchMajor", listSchMajor);
        return JsonData.success(map);
    }
    
    /**
     * 根据专业代码获取学生的分页数据
     * @param majorNo 专业代码
     * @param pageQuery 分页数据
     * @return JsonData
     */
    @RequestMapping("/listStudent")
    @ResponseBody
    public JsonData listStudentWithPage(@RequestParam("majorNo") String majorNo, PageQuery pageQuery) {
        PageInfo<Student> listStudent = studentService.listSchMajorWithPage(majorNo, pageQuery);
        return JsonData.success(listStudent);
    }
    
    /**
     * 跳转到奖惩信息管理页面
     * @param stuNo 学号
     * @param model 
     * @return manager/student/reward/reward_manager.html
     */
    @RequestMapping("/toRewardManager")
    public String toRewardManager(@RequestParam("stuNo")String stuNo, Model model) {
        model.addAttribute("stuNo", stuNo);
        return "manager/student/reward/reward_manager.html";
    }
    
    /**
     * 根据学号获取该学生的奖惩信息列表
     * @param stuNo 学号
     * @return JsonData
     */
    @RequestMapping("/listStuReward")
    @ResponseBody
    public JsonData listStuReward(@RequestParam("stuNo")String stuNo) {
        List<StuReward> listStuReward = studentRewardService.listStuReward(stuNo);
        return JsonData.success(listStuReward);
    }
    
    /**
     * 跳转到添加奖惩信息管理页面
     * @param stuNo 学号
     * @param model 
     * @return manager/student/reward/reward_manager_add.html
     */
    @RequestMapping("/toAddStuReward")
    public String toAddStuReward(@RequestParam("stuNo")String stuNo, Model model) {
        model.addAttribute("stuNo", stuNo);
        return "manager/student/reward/reward_manager_add.html";
    }
    
    /**
     * 保存一条学生奖惩信息
     * @param stuReward 学生奖惩信息
     * @return JsonData
     */
    @RequestMapping("/saveStuReward")
    @ResponseBody
    public JsonData saveStuReward(StuReward stuReward) {
        studentRewardService.saveStuReward(stuReward);
        return JsonData.success();
    }
    
    /**
     * 单个或者批量删除学生奖惩信息
     * @param rewardId 奖惩信息id
     * @return JsonData
     */
    @RequestMapping("/delStuReward")
    @ResponseBody
    public JsonData delStuReward(@RequestParam("rewardId[]")int rewardId[]) {
        if (rewardId.length >= 1) {
            studentRewardService.deleteStuReward(rewardId);
        } else {
            throw new ParamException("rewardId无参数");
        }
        return JsonData.success();
    }
    
    /**
     * 跳转到编辑奖惩信息页面
     * @param rewardId 奖惩id
     * @param model 
     * @return manager/student/reward/reward_manager_edit.html
     */
    @RequestMapping("/toEditStuReward")
    public String toEditStuReward(@RequestParam("rewardId")int rewardId, Model model) {
        StuReward stuReward = studentRewardService.getStuReward(rewardId);
        model.addAttribute("stuReward", stuReward);
        return "manager/student/reward/reward_manager_edit.html";
    }
    
    /**
     * 更新一条奖惩信息
     * @param stuReward 奖惩信息
     * @return JsonData
     */
    @RequestMapping("/updateStuReward")
    @ResponseBody
    public JsonData updateStuReward(StuReward stuReward) {
        studentRewardService.updateStuReward(stuReward);
        return JsonData.success();
    }
}
